import 'package:card_topia/common_model/Bind_card_model.dart';
import 'package:card_topia/design_specifications.dart';
import 'package:card_topia/my_router.dart';
import 'package:card_topia/pages/bind_card_page/bind_card_page.dart';
import 'package:card_topia/utils/dialog_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';



///扫码页面
class ScanPage extends StatefulWidget {
  const ScanPage({super.key});

  @override
  State<ScanPage> createState() => _ScanPageState();
}

class _ScanPageState extends State<ScanPage> {
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');
  PermissionStatus? cameraPermissionStatus;
  QRViewController? _controller;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getCameraPermission();
  }
  getCameraPermission()async{
    cameraPermissionStatus = await Permission.camera.request();
    setState(() {

    });
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _controller?.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        alignment: Alignment.center,
        children: [
          cameraPermissionStatus!=PermissionStatus.granted?Center(
            child: const CupertinoActivityIndicator(color: Colors.white,),
          ):Container(
            width: double.infinity,
            height: double.infinity,
            child: QRView(key: qrKey, onQRViewCreated: (controller){
              _controller = controller;
              _controller?.scannedDataStream.listen((event) {
                _controller?.pauseCamera();
                 DialogUtil.showLoading();
                 Future.delayed(const Duration(seconds: 2),(){
                   DialogUtil.dismiss();
                   _controller?.dispose();
                   MyRouter.replace(page: BindCardPage(model: BindCardModel()));
                   // context.pushReplacement(MyRouter.scanResultPage,extra: {
                   //   "data":event.code
                   // });
                   // return;
                   // context.pushReplacement(MyRouter.bindCardPage,extra: {
                   //   "model":BindCardModel()
                   // });
                   // Navigator.pushReplacement(context, MaterialPageRoute(builder: (_)=>BindCardPage(
                   //   model: BindCardModel(
                   //
                   //   ),
                   // )));
                 });
              });
            },
              overlay: QrScannerOverlayShape(
                  borderColor: themeColor,
                  borderRadius: 1.w,
                  borderLength: 30.w,
                  borderWidth: 8.w,
                  cutOutSize: MediaQuery.of(context).size.width*0.8),
            ),
          ),
          Positioned(
              top: 0,
              left: 0,
              right: 0,
              child: AppBar(
                systemOverlayStyle: SystemUiOverlayStyle.light,
                elevation: 0,
                backgroundColor: Colors.transparent,
                leading: InkWell(
                  onTap:()=>Navigator.pop(context),
                  child: Icon(CupertinoIcons.left_chevron,color: Colors.white,),
                ),
                title: Text("扫描二维码",style: TextStyle(fontFamily: harmony,color: Colors.white,fontSize: 18.sp),),
              )),
          Positioned(
              bottom: MediaQuery.of(context).padding.bottom+50.w,
              child: Container(
              child: Text("请将卡片上的二维码放置在取景框中",style: TextStyle(fontSize: 16.sp,color: Colors.white,fontFamily: harmony),),
          ))
        ],
      ),
    );
  }
}
